//
//  WKBaseViewController.m
//  WeKeTang
//
//  Created by ichina on 2017/8/22.
//  Copyright © 2017年 Dezhi Tiancheng Education Technology (Beijing) Co. Ltd. All rights reserved.
//

#import "WKBaseViewController.h"
#import "AppDelegate.h"
#import "UMMobClick/MobClick.h"
#import "NSString+Addition.h"

@interface WKBaseViewController ()

@end

@implementation WKBaseViewController

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    //友盟统计开始加载视图
    [MobClick beginLogPageView:NSStringFromClass([self class])];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    //友盟统计结束加载视图
    [MobClick endLogPageView:NSStringFromClass([self class])];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.edgesForExtendedLayout = UIRectEdgeNone;//视图控制器，四条边不指定
    self.extendedLayoutIncludesOpaqueBars = NO;//不透明的操作栏
    UINavigationBar *navgationBar = [[UINavigationBar alloc]init];
    navgationBar.frame = CGRectMake(0, 20, kDeviceScreenWidth, 44);
    [self.view addSubview:navgationBar];
    
    [navgationBar setShadowImage:[UIImage new]];
    
    [navgationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys
                                          :[UIColor blackColor], NSForegroundColorAttributeName, [UIFont systemFontOfSize:18], NSFontAttributeName, nil]];
    navgationBar.translucent = NO;
    
    navgationBar.items = @[self.navItem];
    
    self.navgationBar = navgationBar;
    
    UIView *statusbar = [[UIView alloc]init];
    statusbar.backgroundColor = RGBColor(40, 188, 203);
    [navgationBar addSubview:statusbar];
    self.statusbar = statusbar;
}

//设置NavgationBar左面按钮
- (void)setLeftButtonWithImage:(NSString*)imageButtonName
              highlightedImage:(NSString*)highlightedImageButtonName
                        target:(id)target action:(SEL)action{
    
    UIButton *tmpButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [tmpButton setImage:[UIImage imageNamed:imageButtonName] forState:UIControlStateNormal];
    [tmpButton setImage:[UIImage imageNamed:imageButtonName] forState:UIControlStateHighlighted];
    [tmpButton setTintColor:[UIColor whiteColor]];
    if (action) {
        [tmpButton addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    }
    tmpButton.frame = CGRectMake(5, (44-35)*0.5, 35, 35);
    [self.navgationBar addSubview:tmpButton];
}
- (void)setRightButtonWithNomalImageName:(NSString*)NomalImageName
                    highlightedImageName:(NSString*)highlightedImageName
                                  target:(id)target action:(SEL)action{
    UIButton *tmpBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [tmpBtn setImage:[UIImage imageNamed:NomalImageName] forState:UIControlStateNormal];
    [tmpBtn setTintColor:[UIColor whiteColor]];
    [tmpBtn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    tmpBtn.frame = CGRectMake(kDeviceScreenWidth-35, (44-35)*0.5, 35, 35);
    [self.navgationBar addSubview:tmpBtn];
}
//设置NavgationBar右面按钮文字
- (void)setRightButtonWithTittle:(NSString*)tittle
                           color:(UIColor *)color
                          target:(id)target
                          action:(SEL)action{
    
    UIButton *tmpBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    [tmpBtn setTintColor:[UIColor whiteColor]];
    [tmpBtn setTitleColor:color forState:UIControlStateNormal];
    [tmpBtn setTitle:tittle forState:UIControlStateNormal];
    [tmpBtn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    [tmpBtn.titleLabel setFont:[UIFont systemFontOfSize:15]];
    [tmpBtn.titleLabel setTextAlignment:NSTextAlignmentRight];
    if (tittle.length >2) {
        tmpBtn.frame = CGRectMake(0, 0, 80, 30);
    }else{
        tmpBtn.frame = CGRectMake(0, 0, 40, 30);
    }
    tmpBtn.frame = CGRectMake(kDeviceScreenWidth-40, (44-35)*0.5, 35, 35);
    [self.navgationBar addSubview:tmpBtn];
}
- (void)middleTitleColor:(UIColor*)colcor{
    
    [self.navgationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys
                                               :colcor, NSForegroundColorAttributeName, [UIFont systemFontOfSize:18], NSFontAttributeName, nil]];
}
- (void)backAction{
    [MobClick profileSignInWithPUID:@"binding_back"];
    [self.navigationController popViewControllerAnimated:YES];
}
//返回可选控制器
-(void)popToViewControllerWithClass:(Class)aClass{
    
    NSArray *viewControllers = [self.navigationController viewControllers];
    
    NSUInteger viewControllersCount = viewControllers.count;
    
    for(int i = 0; i < viewControllersCount; i++){
        
        id viewController = viewControllers[i];
        
        if([viewController isKindOfClass:aClass] ){
            
            [self.navigationController popToViewController:viewController animated:YES];
            break;
        }
    }
}
- (void)showInfo:(NSString*)info{
    
    YYLabel *label = [[YYLabel alloc]init];
    label.text = info;
    label.textAlignment = NSTextAlignmentCenter;
    label.textColor = UIColor.whiteColor;
    label.backgroundColor = [UIColor colorWithWhite:0 alpha:0.7];
    label.layer.cornerRadius = 5;
    label.layer.masksToBounds = YES;
    label.numberOfLines = 0;
    label.font = [UIFont systemFontOfSize:16];
    CGSize size = [label.text bd_sizeWithFont:[UIFont systemFontOfSize:18] textMaxSize:CGSizeMake(kDeviceScreenWidth, 300)];
    
    label.size = CGSizeMake(size.width, size.height*2);
    label.center =self.view.center;
    [self.view addSubview:label];
    label.transform = CGAffineTransformMakeScale(1.2, 1.2);
    [UIView animateWithDuration:1.5 animations:^{
        
        label.alpha = 0;
        
    }completion:^(BOOL finished) {
        
        [UIView animateWithDuration:0.25 animations:^{
            
            label.transform = CGAffineTransformIdentity;
            [label removeFromSuperview];
        }];
    }];
}
//屏幕适配
- (void)outLayoutSelfSubviews{
    
    for (UIView *views in self.view.subviews) {
        
        views.frame = WTCGRectMake(views.frame);
        
        if ([views isKindOfClass:[UIWebView class]]) {
            continue;
        }
        if ([views isKindOfClass:[UIButton class]]) {
            continue;
        }
        if ([views isKindOfClass:[UISearchBar class]]){
            continue;
        }
        for (UIView *subViews in views.subviews) {
            subViews.frame = WTCGRectMake(subViews.frame);
        }
    }
}
- (void)showIndicatorWithFlag:(BOOL)flag title:(NSString *)title{
    
    UIView *activityView = (UIView *)[theApp.window viewWithTag:70021];
    if (flag) {
        if (activityView==nil) {
            activityView = [[UIView alloc] initWithFrame:CGRectMake(110, 120, 100, 100)];
            activityView.tag = 70021;
            activityView.backgroundColor = [UIColor clearColor];
            UIView * v = [[UIView alloc]initWithFrame:activityView.bounds];
            v.backgroundColor = [UIColor blackColor];
            v.alpha = .7;
            v.layer.cornerRadius = 5;
            v.layer.masksToBounds = YES;
            [activityView addSubview:v];
            [activityView.layer setContents:(id)[[UIImage imageNamed:@"incativy.png"] CGImage]];
            
            UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(3, 65, 95, 32)];
            label.textAlignment = NSTextAlignmentCenter;
            label.text = title;
            label.numberOfLines = 0;
            label.font = [UIFont systemFontOfSize:12];
            label.textColor = [UIColor whiteColor];
            label.backgroundColor = [UIColor clearColor];
            [activityView addSubview:label];
            
            UIActivityIndicatorView * imgv = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
            
            imgv.frame = CGRectMake((100-26)/2, 30, 26, 26);
            
            [activityView addSubview:imgv];
            imgv.tag = 794232;
            [imgv startAnimating];
            activityView.center = theApp.window.center;
            [[[UIApplication sharedApplication] delegate].window addSubview:activityView];
        }
    }
    else{
        if (activityView && [activityView superview]) {
            UIImageView *activityIndicator = (UIImageView *)[activityView viewWithTag:794232];
            if (activityIndicator) {
                [activityIndicator stopAnimating];
            }
            [activityView removeFromSuperview];
        }
    }
}
- (void)showCustomAlertMessage:(NSString*)message{
    if (message==nil || [message length]<1) {
        return;
    }
    WKCustomAlertView *customAlartView = (WKCustomAlertView *)[theApp.window viewWithTag:980153];
    if (customAlartView) {
        [customAlartView removeFromSuperview];
        customAlartView = nil;
    }
    customAlartView = [[WKCustomAlertView alloc] initWithFrame:CGRectMake(45*(kDeviceScreenWidth/320.0), 460 / 2, 230, 50)] ;
    customAlartView.center = theApp.window.center;
    if (iPhone4) {
        customAlartView.center = CGPointMake(theApp.window.center.x, theApp.window.center.y - 60);
    }
    customAlartView.titleLabel.text = message;
    customAlartView.tag = 980153;
    
    [theApp.window addSubview: customAlartView];
}
#pragma mark -懒加载navItem
- (UINavigationItem *)navItem{
    
    if (!_navItem) {
        
        _navItem = [[UINavigationItem alloc]init];
        
    }
    return _navItem;
}
- (void)viewDidLayoutSubviews{
    
    [super viewDidLayoutSubviews];
    
    self.statusbar.frame = CGRectMake(0, -20, kDeviceScreenWidth, 20);
}
@end

